var Task_enrol_list = require('../model/task_enrol_list')
const TaskList = require('../model/taskList').TaskList
const User_message_list = require('../model/user_message_list')
const User = require('../model/user').User
const axios = require('axios')
const unique = require('../utils/unique')
const md5 = require('md5')
var validator = require('validator');
const result = require('../utils/result')
const Sequelize = require('sequelize')
const uniqeByKeys =require('../utils/uniqeByKeys')
const Op = Sequelize.Op
const user_message_list = require('../model/user_message_list')

/**
 * 报名列表
 * @param req
 * @param res
 * @returns {Promise<void>}
 * @constructor
 */
exports.Task_enrol_list = async (req,res)=>{
    let uid = req.query.uid;
    let taskid = req.query.taskid;
    let R= await Task_enrol_list.findAll({
        where: {
            t_uid:req.query.uid,
            isCheck:0,
            taskid:taskid
        }})
    if(R){
        res.json(result(0,'success',R))
    }else{
        res.json(result(-1,'fails','获取失败'))
    }
}
/**
 * 任务报名
 * @param req
 * @param res
 * @returns {Promise<void>}
 * @constructor
 */
exports.Task_enrol = async (req,res)=>{
    const u_info = await User.findOne({where: {uid:req.query.uid}});
    const t_info = await TaskList.findOne({where:{id:req.query.taskid}})
    const g_result = await Task_enrol_list.findOne({
        where:{
            taskid: req.query.taskid,
            uid:req.query.uid
        }
    });
    if(!g_result){
        const headimgurl = u_info.headimgurl ||''
        const nickname = u_info.nickname ||''
        const user_name = u_info.username ||''
        const uid = req.query.uid
        const taskid = req.query.taskid
        const t_uid=t_info.uid
        const name = t_info.name
        const demand =t_info.demand
        const describe =t_info.describe
        const time =t_info.time
        const price =t_info.price
        const links1 =t_info.links1
        const links2 =t_info.links2
        const img_url1 =t_info.img_url1
        const img_url2 =t_info.img_url2
        const address =t_info.address
        const type =t_info.type

        if(!taskid || taskid =='' || !uid || uid ==''){ res.json(result(-1,'fail','错误'));return}
        Task_enrol_list.create({
            headimgurl,nickname,user_name,uid,taskid,t_uid,demand,
            describe,price,time,links1,links2,img_url1,img_url2,address,name,
            type
        }).then(v=>{
            res.json(result(0,'success','报名成功'))
          /*  User.findOne({where:{uid:t_info.uid}}).then(r=>{
                axios.get('http://localhost:3100/wx/message',{params:{
                        select:'sigin_call_suc',
                        openid:r.openid,
                        name:t_info.name,
                        person:u_info.nickname,
                        sex:"未知",
                        remark:"请登陆平台查看详情"
                    }}).then(rs=>{

                })
            })*/

        }).catch(err=>{
            res.json(result(-1,'fail','报名失败'))
        })

    }else{
        res.json(result(-1,'fail','该任务你经报名了,不能重复报名哦'))
    }


}
/**
 * 任务授权
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.task_auth = async (req,res) =>{
    const uid = req.query.uid;const taskid = req.query.taskid;const t_uid= req.query.t_uid;
    if(!uid || !t_uid ||!taskid){
        res.json(result(-1,'fail','缺少必要参数'))
        return;
    }
    let _g = await Task_enrol_list.findOne({where:{
            taskid:taskid,
            uid:uid,
            isCheck: 0,
        }});
    if(_g !==null){
        TaskList.update({
            belong:uid,
            status:2
        },{where:{id:taskid}})
        Task_enrol_list.update({
            isCheck:1
        },{where:{
                taskid:taskid, t_uid:t_uid,}})

        User.findOne({where:{uid:req.query.uid}}).then(r=>{
            axios.get('http://localhost:3100/wx/message',{params:{
                    select:'task_sigin',
                    first:"您好"+r.nickname+",您的项目申请状态已更新",
                    openid:r.openid,
                    name:'知行兼职项目报名申请',
                    status:"已通过",
                    remark:"项目申请通过审核,请登录知行兼职参与项目"
                }}).then(rs=>{
                // res.json(result(0,'success','状态改为审核通过'))

            })
        })
        res.json(result(0,'success','任务授权成功'))
    }else{
        res.json(result(-1,'fail','任务授权失败'))
    }
}
/**
 * 用户请求任务完结
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.task_finish_first = async (req,res) =>{
    const uid = req.query.uid;const taskid = req.query.taskid;
    let _g = await TaskList.findOne({where:{
            id:taskid,
            status:2
        }});
    if(_g !==null){
        TaskList.update({
            status:3
        },{where:{id:taskid}})
        res.json(result(0,'success','您的任务已完结,等待甲方确认'))

        let rs = await TaskList.findOne({where:{id:taskid}})
        let rs2 = await User.findOne({where:{uid:rs.uid}})
        User.findOne({where:{uid:uid}}).then(r=>{
            axios.get('http://localhost:3100/wx/message',{params:{
                    select:'task_finish',
                    openid:rs2.openid,
                    first:"你有新的目标完成待确认",
                    conent:"",
                    type:"",
                    user:r.nickname,
                    remark:"请登陆平台及时处理"
                }}).then(rs=>{
                //...
            })
        })
    }else{
        res.json(result(-1,'fail','请求错误'))
    }
}
/**
 * 甲方确认任务完结
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.task_finish_second = async (req,res) =>{
    const uid = req.query.uid;const taskid = req.query.taskid;
    let _g = await TaskList.findOne({where:{
            id:taskid,
            status:3
        }});
    if(_g !==null){
        TaskList.update({
            status:4
        },{where:{id:taskid}})
        res.json(result(0,'success','您已经同意任务完结'))
        let p = await User.findOne({where:{uid:uid}})
        User.findOne({where:{uid:_g.belong}}).then(r=>{
            axios.get('http://localhost:3100/wx/message',{params:{
                    select:'task_finish2',
                    openid:r.openid,
                    first:"甲方已同意您的任务完结请求",
                    content:"",// 验收内容：制作新年祝福贺卡任务
                    status:"雇主已经验收成功",//验收结果：雇主已经验收成功!
                    user:p.nickname,
                    remark:"请登陆平台查看哦"
                }}).then(rs=>{
                //...
            })
        })

    }else{
        res.json(result(-1,'fail','对方尚未同意完结'))
    }
}

/**
 * 工作台- 进行中
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.task_user_doing =async (req,res)=>{
    let uid = req.query.uid;

    let  _g = await TaskList.findAll({
        where:{
            belong:req.query.uid,
            status:{
                [Op.in]: [2, 3]
            }
        }
    })
    if(_g && _g.length>0 ){
        res.json(result(0,'success',_g))
    }else{
        res.json(result(0,'success',_g))
    }

}

/**
 * 工作台- 已报名
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.task_user_sign =async (req,res)=>{
    let uid = req.query.uid;
    let  _g = await Task_enrol_list.findAll({
        where:{
            uid:uid,
            isCheck:0
        }
    })
    if(_g && _g.length>0 ){
        res.json(result(0,'success',_g))
    }else{
        res.json(result(0,'success',_g))
    }

}
/**
 * 工作台- 已完成
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.task_user_end =async (req,res)=>{
    let uid = req.query.uid;

    let  _g = await TaskList.findAll({
        where:{
            belong:uid,
            status:4
        }
    })
    if(_g && _g.length>0 ){
        res.json(result(0,'success',_g))
    }else{
        res.json(result(0,'success',_g))
    }

}

/**
 * 发布- 已完成
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.task_finish =async (req,res)=>{
    let uid = req.query.uid;

    let  _g = await TaskList.findAll({
        where:{
            uid:uid,
            status:4
        }
    })
    if(_g && _g.length>0 ){
        res.json(result(0,'success',_g))
    }else{
        res.json(result(0,'success',_g))
    }

}

/**
 * 发布- 进行中
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.task_doing =async (req,res)=>{
    let uid = req.query.uid;
    let  _g = await TaskList.findAll({
        where:{
            belong:{[Op.gte]: 0, },
            uid:uid,
            status:{
                [Op.ne]: 4,
            }
        }
    })
    if(_g && _g.length>0 ){
        res.json(result(0,'success',_g))
    }else{
        res.json(result(0,'success',_g))
    }

}
/**
 * 发布-报名中
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.task_sign =async (req,res)=>{
    let uid = req.query.uid;
   //  let G =  await Task_enrol_list.findAll({where:{t_uid: uid}})
    //    //  let G2 = await TaskList.findAll({where:{uid:uid,isDel:0,status:{[Op.in]: [0, 1]}}})
    //    //  let r = []
    //    //  g.forEach(function (el,i) {
    //    //      G2.forEach(function (ele,idx) {
    //    //          if(el.taskid ==ele.id){
    //    //              r.push(ele)
    //    //          }
    //    //      })
    //    //  })
    //    // r = uniqeByKeys(r,'id')
    let id_arry = await Task_enrol_list.findAll({where:{t_uid:uid}})
    let _arry = [];
    uniqeByKeys(id_arry,['taskid']).forEach(function (item,idx) {
        _arry.push(item.taskid)
    })
    let task_arry = await TaskList.findAll({where:{id:{[Op.in]: _arry},uid:uid,status:{[Op.in]:[0,1]}}})
   res.json(result(0,'',task_arry))

}

